package com.itwn.portal.web;



import com.github.pagehelper.PageInfo;
import com.itwn.portal.entity.Resource;
import com.itwn.portal.reult.ResponseResult;
import com.itwn.portal.service.ResourceService;

import com.itwn.portal.vo.ResourceVo;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("resource")
public class ResourceController {

    @Autowired
    ResourceService resourceService;

    //查询是否推荐
    /**
     *
     * @param num
     * @return
     */
    @RequestMapping("/listFine")
    @ResponseBody
    public List<ResourceVo> queryFine(Integer num){
       List<ResourceVo> list= resourceService.selectByFine(num);
        return list;
    };

    //查询销量
    /**
     *
     * @param num
     * @return
     */
    @RequestMapping("/listBuy")
    @ResponseBody
    public List<ResourceVo> queryBuy(@RequestParam(defaultValue = "1") Integer num){
        List<ResourceVo> list= resourceService.queryBuy(num);
        return list;
    };

    /**
     *
     * @param num
     * @return
     */
    //根据红票查询
    @RequestMapping("/listRed")
    @ResponseBody
    public List<ResourceVo> queryRed(Integer num){
        List<ResourceVo> list= resourceService.queryRed(num);
        return list;
    };

    //随机查四个
    @RequestMapping("/listCons")
    @ResponseBody
    public List<ResourceVo> queryByCons(){
        List<ResourceVo> list= resourceService.queryByCons();
        return list;
    };
    /**
     *
     * @param num
     * @return
     * 月票榜
     */
    @RequestMapping("/listByMonth")
    @ResponseBody
    public ResponseResult<List<ResourceVo>> queryMonth(Integer num){
        ResponseResult<List<ResourceVo>> result = new ResponseResult<>();
        try {
            List<ResourceVo> list = resourceService.queryMonth(num);
            result.setMsg("查询成功!");
            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败!");
            result.setCode(201);
        }
        return result;
    };
    /**
     *
     * @param num
     * @return
     * 收藏榜
     */
    @RequestMapping("/listByCollect")
    @ResponseBody
    public ResponseResult<List<ResourceVo>> queryCollect(Integer num){
        ResponseResult<List<ResourceVo>> result = new ResponseResult<>();
        try {
            List<ResourceVo> list = resourceService.queryCollect(num);
            result.setMsg("查询成功!");
            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败!");
            result.setCode(201);
        }
        return result;
    };
    @RequestMapping("/listSex")
    @ResponseBody
    public ResponseResult<List<ResourceVo>> querySex(Integer num){
        ResponseResult<List<ResourceVo>> result = new ResponseResult<>();
        try {
            List<ResourceVo> list = resourceService.querySex(num);
            result.setMsg("查询成功!");
            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败!");
            result.setCode(201);
        }
        return result;
    };
    @RequestMapping("/getPersonCollect")
    @ResponseBody
    public ResponseResult<List<ResourceVo>> queryPersonCollect(Integer uid){
        ResponseResult<List<ResourceVo>> result = new ResponseResult<>();
        try {
            List<ResourceVo> list = resourceService.queryPersonCollect(uid);
            result.setMsg("查询成功!");
            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败!");
            result.setCode(201);
        }
        return result;
    };
    @RequestMapping("/getPersonCollectTotal")
    @ResponseBody
    public ResponseResult<List<Integer>> queryPersonCollectTotal(Integer uid){
        ResponseResult<List<Integer>> result = new ResponseResult<>();
        try {
            List<Integer> list = resourceService.queryTotalByUid(uid);
            result.setMsg("查询成功!");
            result.setCode(200);
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败!");
            result.setCode(201);
        }
        return result;
    };


    @RequestMapping("/listResource")
    @ResponseBody
    public ResponseResult<PageInfo<ResourceVo>> initResource(@RequestParam(value = "now", defaultValue = "1")Integer now,
                                                             @RequestParam(value = "size", defaultValue = "3")Integer size, String name, Integer tagId, Integer num,Integer orders){
        ResponseResult<PageInfo<ResourceVo>> result = new ResponseResult<>();
        try {
            PageInfo<ResourceVo> pageInfo = resourceService.initAllResource(now, size, name, tagId,num,orders);
            result.setMsg("查询成功!");
            result.setCode(200);
            result.setData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败!");
            result.setCode(201);
        }
        return result;
    };


    @RequestMapping("/selectById")
    @ResponseBody
    public ResponseResult<Resource> queryById(Integer num){
        ResponseResult<Resource> result = new ResponseResult<>();
        try {
            Resource resource = resourceService.queryById(num);
            System.out.println("resource=="+resource);
            result.setMsg("查询成功!");
            result.setCode(200);
            result.setData(resource);
        }catch(Exception e){
            e.printStackTrace();
            result.setMsg("查询失败!");
            result.setCode(201);
        }
        return result;
    }

    /**
     * 投月票
     * @param uid
     * @param rid
     * @return
     */
    @GetMapping("/voteMTicket")
    @ResponseBody
    public ResponseResult<Void> voteMTicket(Integer uid,Integer rid){
        System.out.println("投月票中!"+uid+":"+rid);
        ResponseResult<Void> result = new ResponseResult<>();
        try{
            this.resourceService.voteMTicket(rid,uid);
            result.setMsg("投票成功!");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("投票失败!");
        }
        return result;
    }

    @GetMapping("/voteThumb")
    @ResponseBody
    public ResponseResult<Void> voteColor(Integer colornum,Integer rid){
        System.out.println("投红黑票中!"+colornum+":"+rid);
        ResponseResult<Void> result = new ResponseResult<>();
        try{
            this.resourceService.voteColor(rid,colornum);
            result.setMsg("投票成功!");
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("投票失败!");
        }
        return result;
    }

    @GetMapping("/selectFirstSectionId")
    @ResponseBody
    public ResponseResult<Integer> selectFirstSectionId(Integer rid){
        System.out.println("查该资源第一章的id中 : :"+rid);
        ResponseResult<Integer> result = new ResponseResult<>();
        try{
            int firstId = this.resourceService.queryFirstSectionId(rid);
            System.out.println("第一章的id是=========》"+firstId);
            result.setMsg("查询成功!");
            result.setCode(200);
            result.setData(firstId);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }


}

